package com.swipemaster.network.task;

import java.util.ArrayList;

import android.app.Activity;

import com.swipemaster.dialogs.Dialogs;
import com.swipemaster.network.Network;
import com.swipemaster.network.WorldRecord;
import com.swipemaster.record.RecordId;

/** Downloading and showing first world records in dialog. */
public class ShowRecordsTask extends ProgressTask<Void, Void, ArrayList<WorldRecord>>
{
	private static final int FIRST_RECORDS_PORTION = 10;
	private final Activity activity;
	private final RecordId recordId;

	public ShowRecordsTask(Activity activity, RecordId recordId)
	{
		super(activity);
		this.activity = activity;
		this.recordId = recordId;
	}

	@Override
	protected ArrayList<WorldRecord> doInBackground(Void... params)
	{
		if (SendStatsTask.shouldSendStats())
		{
			new SendStatsTask().execute();
		}

		try
		{
			return Network.getWorldRecords(recordId, 0, FIRST_RECORDS_PORTION);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onPostExecute(ArrayList<WorldRecord> result)
	{
		super.onPostExecute(result);

		if (result == null)
		{
			Dialogs.showInternetError(activity);
			return;
		}

		Dialogs.showRecords(activity, recordId, result);
	}
}
